<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\UsersService;
use app\model\ScoreGoods;
use app\model\ScoreGoodsOrders;
use app\model\ScoreGoodsOrdersAssess;
use app\model\ScoreGoodsSku;
use app\model\Users;
use app\Request;
use think\db\exception\DbException;
use think\db\Query;
use think\response\Json;

class ScoreGoodsController extends BaseController
{
	
	protected array $middleware = ['Login' => [
		'except' => ['index', 'tags'],
	]];
	
	/**
	 * 列表
	 * @return Json
	 */
	public function index()
	{
		$rows = ScoreGoods::order(['weigh'=>'desc'])->paginate()->items();
		writeJson($rows);
	}
	
	
	/**
	 * 积分支付
	 * @param Request $request
	 * @return Json
	 */
	public function pay(Request $request)
	{
		$params = $request->param(); //id
		$goods = ScoreGoods::findOrFail($params['id']);
		if ($goods->num < $params['num']) {
			writeJson(0, '库存不足');
			
		}
		$score = $goods->score * $params['num'];//支付积分
		try {
			UsersService::changeScore(-$score, $request->users_id, '兑换商品');
		} catch (\Exception $e) {
			writeJson(0, $e->getMessage());
		}
		$params['goods_id'] = $params['id'];
		unset($params['id']);
		$params['order_sn'] = get_uniqid();
		$params['score'] = $score;
		$res = ScoreGoodsOrders::create($params);
		$goods->sell_num += 1;
		$goods->num -= 1;
		$goods->save();
		
		writeJson(1, 'ok', $res->id);
	}
	
	/**
	 * 详情
	 * @param int $id
	 * @return Json
	 */
	public function read(int $id)
	{
		$row = ScoreGoods::withCount('assess')->filter(function ($data) {
			$sku = explode(',', $data->sku_ids);
			$res = ScoreGoodsSku::whereIn('id', $sku)->with(['sku'])->select();
			$data->sku = $res;
		})->find($id);
		writeJson($row);
	}
	
	/**
	 * 评价列表
	 * @param int $id
	 * @return Json
	 */
	function assess(int $id)
	{
		$rows = ScoreGoodsOrdersAssess::with(['users'])
			->filter(function ($data) {
				$data->rate = round(($data->depict + $data->serve + $data->logistics) / 3);
			})
			->where(['goods_id' => $id])
			->order(['id' => 'desc'])
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 订单列表
	 * @param Request $request
	 * @param int $status
	 * @return Json
	 */
	function order_list(Request $request, int $status)
	{
		$rows = ScoreGoodsOrders::when(!empty($status), function (Query $query) use ($status) {
			if (in_array($status, [1, 2, 3, 4])) {
				$query->where(['status' => $status]);
			}
		})->with(['goods'])->filter(function ($data) {
			$sku2 = ScoreGoodsSku::find($data->sku_id);
			$sku1 = ScoreGoodsSku::find($sku2->pid);
			$data->sku = $sku1->name . '-' . $sku2->name;
		})->where(['users_id' => $request->users_id])->order(['id' => 'desc'])->paginate()->items();
		writeJson($rows);
	}
	
	/**
	 * 订单详情
	 * @param int $id
	 * @return Json
	 */
	function order_detail(int $id)
	{
		$row = ScoreGoodsOrders::with(['goods', 'address'])->filter(function ($data) {
			$sku2 = ScoreGoodsSku::find($data->sku_id);
			$sku1 = ScoreGoodsSku::find($sku2->pid);
			$data->sku = $sku1->name . '-' . $sku2->name;
		})->find($id);
		writeJson($row);
	}
	
	/**
	 * 确认收货
	 * @param int $id
	 * @return \think\response\Json
	 */
	function confirm_receipt(int $id)
	{
		ScoreGoodsOrders::where(['id' => $id])->update(['status' => 3]);
		writeJson();
	}
	
	/**
	 * 订单评价
	 * @param Request $request
	 * @return Json
	 */
	function order_assess(Request $request)
	{
		$params = $request->param();
		$row = ScoreGoodsOrders::find($params['id']);
		$data = [
			'order_id' => $params['id'],
			'users_id' => $request->users_id,
			'goods_id' => $row->goods_id,
			'depict' => $params['depict'],
			'serve' => $params['serve'],
			'logistics' => $params['logistics'],
			'content' => $params['content'],
			'images' => $params['images'],
		];
		ScoreGoodsOrdersAssess::create($data);
		$row->status = 4;
		$row->save();
		writeJson();
	}
	
	/**
	 * 删除订单
	 * @param int $id
	 * @return \think\response\Json
	 */
	function order_delete(int $id)
	{
		ScoreGoodsOrders::destroy($id);
		writeJson();
	}
}
